<?php

namespace app\Modules\Admin\controllers;



use Yii;
use app\models\Showorder;
use app\models\ShoworderSearch;
use app\models\UploadForm;
use app\models\Saleorder;
use app\models\Showpic;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * BannerController implements the CRUD actions for Banner model.
 */
class ShoworderController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all Banner models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new ShoworderSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Banner model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Banner model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate($order_id)
    {
        $model = new Showorder();
        $order = Saleorder::findOne($order_id);
        $user = $order->user;
        $model->uname = $user->name;
        $model->uimg = $user->img;
        $model->order_id = $order_id;
        $model->auction_id = $order->auction_id;
        $model->product_id = $order->product_id;
        $model->user_id = $order->user_id;

        if (($model->load(Yii::$app->request->post()) ) && $model->save()) {
            Showpic::updateAll(['show_id'=>$model->showid],['auction_id'=>$model->auction_id,'show_id'=>0]);
            $order->state = Saleorder::STEAT_POST;
            $order->save(false);
            return $this->redirect(['index']);
        }
        $uploadForm = new UploadForm();
        $showpic = new Showpic();
        $showpic->auction_id = $order->auction_id;
        $showpic->product_id = $order->product_id;
        $showpic->user_id = $order->user_id;


        return $this->render('create', [
            'model' => $model,
            'showpic'=>$showpic,
            'uploadForm'=>$uploadForm,
        ]);
    }

    /**
     * Deletes an existing Banner model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the Banner model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Showorder the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Showorder::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
